<?php
	require '../../lib/init.php';
	$gCn=Con();	
	if($_GET['action']=='delete') {
		$objid=(int)$_GET['objid'];		
		$sql='delete from campaigns where campaign_id='.$objid.'';
		$gCn->Execute($sql);
		header('Location: viewCamp.php');
	}
	
	if($_POST['name'] && $_POST['client_id']) {
		$sql='insert into campaigns (name,client_id,thumb_src,descr) values (\''.$_POST['name'].'\','.$_POST['client_id'].',\''.$_POST['thumb_src'].'\',\''.$_POST['descr'].'\');';
		$gCn->Execute($sql);	
	}
	
	$sql='select * from clients;';
	$gCn->Execute($sql);
	$gCn->MoveFirst();
	$clients_options='';
	while(!$gCn->EOF()) {
		$clients_options.='<option value="'.$gCn->mRs['client_id'].'"';
		if($client_id==$gCn->mRs['client_id']) $clients_options.=' selected';
		$clients_options.='>'.$gCn->mRs['name'].'</option>';
		$gCn->MoveNext();
	}
	
	
?>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert Object</title>
</head>
<body align="center">
	<br><br>
	<table border="1" width="90%" align="center">
		<tr><th width="40px">ID</th><th>Име</th><th>Descr</th><th width="240px" >Actions</th></tr>
		<?php
			$sql='select * from campaigns order by campaign_id desc;';
			$gCn->Execute($sql);
			$gCn->MoveFirst();
			while(!$gCn->EOF()) {
				echo '<tr><td>'.$gCn->mRs['campaign_id'].'</td><td>'.$gCn->mRs['name'].'</td><td>'.$gCn->mRs['descr'].'</td><td><a href="viewCamp.php?action=delete&objid='.$gCn->mRs['campaign_id'].'">DELETE</a></td></tr>';
				$gCn->MoveNext();
			}
			$gCn->Close();
		?>
		
	</table>
	<br><br>
	<br><br>
	<form name="form1" method="post" action="viewCamp.php">
			Име: <input type="text" name="name"></input><br><br>
			Клиент:	<select name="client_id">
					<option value="-1"></option>
					<?=$clients_options?>
				</select><br><br>
			Thumb: <input type="text" name="thumb_src"></input> <br><br>
			Описание: <br><textarea name="descr" cols="40" rows="8"></textarea><br><br>
			<input type="submit" value="Create Campaign"></input>			
		</form>

	
	
	
</body>
</html>